package handler

import (
	"gitee.com/mangenotwork/game-douniu/api/dao"
	"gitee.com/mangenotwork/game-douniu/api/entity"
	"github.com/mangenotwork/common/ginHelper"
	"github.com/mangenotwork/common/log"
	"github.com/mangenotwork/common/utils"
)

func IosAdd(c *ginHelper.GinCtx) {
	visitor, err := c.Cookie("visitor")
	if err != nil {
		log.Info("err", err.Error())
	}

	uuid := c.Query("uuid")

	has, err := new(dao.IosUUIDDao).GetByUuid(uuid)
	if has.Id > 0 {
		c.APIOutPut(has, "ok")
		return
	}

	_, err = new(dao.IosUUIDDao).Add(uuid, visitor)
	if err != nil {
		c.APIOutPutError(err, err.Error())
		return
	}
	c.APIOutPut("ok", "ok")
	return
}

func IosLink(c *ginHelper.GinCtx) {

}

type IosListResp struct {
	Total int64             `json:"total"`
	List  []*entity.IosUUID `json:"list"`
}

func IosList(c *ginHelper.GinCtx) {
	pageStr := c.Query("page")
	page := utils.AnyToInt64(pageStr)
	limitStr := c.Query("limit")
	limit := utils.AnyToInt64(limitStr)
	count, data, err := new(dao.IosUUIDDao).List(page, limit)
	if err != nil {
		c.APIOutPutError(err, err.Error())
		return
	}
	resp := &IosListResp{
		Total: count,
		List:  data,
	}
	c.APIOutPut(resp, "ok")
	return
}

func IosAgree(c *ginHelper.GinCtx) {
	uuid := c.Query("uuid")
	agreeStr := c.Query("agree")
	agree := utils.AnyToInt64(agreeStr)

	err := new(dao.IosUUIDDao).Agree(uuid, agree)
	if err != nil {
		c.APIOutPutError(err, err.Error())
		return
	}
	c.APIOutPut("ok", "ok")
	return
}

func IosAgreeState(c *ginHelper.GinCtx) {
	visitor, err := c.Cookie("visitor")
	if err != nil {
		log.Info("err", err.Error())
	}

	data, err := new(dao.IosUUIDDao).GetByVisitor(visitor)
	if err != nil {
		c.APIOutPutError(err, err.Error())
		return
	}
	c.APIOutPut(data, "ok")
	return
}
